package com.wgts.wgts.mvp.view.activity.startpage;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.NetLoadingDialog;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerStartPageComponent;
import com.wgts.wgts.dagger.module.StartPageModule;
import com.wgts.wgts.mvp.presenter.IStartPagePersenter;
import com.wgts.wgts.mvp.view.IStartPageView;
import com.wgts.wgts.mvp.view.activity.advertisement.AdvertisementActivity;
import com.wgts.wgts.mvp.view.activity.bootpage.BootPageActivity;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.netmodle.resp.AdvertisingEntity;
import com.wgts.wgts.tools.AppInfor;

import javax.inject.Inject;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class StartPageActivity extends BaseActivity implements IStartPageView {

    @Inject
    IStartPagePersenter pagePersenter;

    private AdvertisingEntity entity;

    private NetLoadingDialog dialog;

    @Override
    public void init() {
        DaggerStartPageComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .startPageModule(new StartPageModule(this))
                .build()
                .inject(this);
    }



    @Override
    public void onCreate(Bundle saveInstanceBundle){
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.start_page_activity);

        dialog = new NetLoadingDialog(StartPageActivity.this);
        StartPageActivityPermissionsDispatcher.needPermissionWithPermissionCheck(this);
    }


    @NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.READ_EXTERNAL_STORAGE})
    public void needPermission(){
        AppInfor.getInstance().setIMEI(this);
       // pagePersenter.jumpTime();
        if (dialog != null) {
            dialog.show();
        }
        pagePersenter.pingIP();
    }

    @OnNeverAskAgain({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.READ_EXTERNAL_STORAGE})
    public void openPermission(){
        MoHuToast.showToast("拒绝权限部分功能将不能使用，请去设置打开权限");
        //pagePersenter.jumpTime();
        if (dialog != null) {
            dialog.show();
        }
        pagePersenter.pingIP();
    }

    @OnPermissionDenied({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.READ_EXTERNAL_STORAGE})
    public void deniedPermission(){
        MoHuToast.showToast("拒绝权限部分功能将不能使用，请去设置打开权限");
        //pagePersenter.jumpTime();
        if (dialog != null) {
            dialog.show();
        }
        pagePersenter.pingIP();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        StartPageActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }


    @Override
    public void jumpResult(boolean jumpResult) {
        pagePersenter.removeTime();
        Log.d("", "");
        //true不需要进入引导页， false 需要进入

        if(jumpResult){
            if(entity != null && (!entity.normalImg.equals("") || !entity.bigImg.equals(""))){
                Intent intent = new Intent(this, AdvertisementActivity.class);
                intent.putExtra(AdvertisementActivity.ADVERTISEMENT, entity);
                startActivity(intent);
            }else{
                Intent intent = new Intent(this, MoHuMainActivity.class);
                startActivity(intent);
            }
        }else{

            Intent intent = new Intent(this, BootPageActivity.class);
            startActivity(intent);
        }


        finish();
    }

    @Override
    public void jumpTime() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                pagePersenter.jumpResult();
            }
        });
    }

    @Override
    public void advertising(AdvertisingEntity entity) {
        this.entity = entity;
    }

    @Override
    public void pingResult() {
        pagePersenter.advertising();
        pagePersenter.jumpTime();
    }

    @Override
    public void onDestroy(){
        pagePersenter.removeTime();
        super.onDestroy();
    }

    @Override
    public void onError(int status, String msg) {

    }
}
